import RPi.GPIO as GPIO
import time
import os,sys
pinScreen=27
p1=5
p2=24
p3=22
p4=23
p5=4
p6=17
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(pinScreen,GPIO.OUT)
GPIO.output(pinScreen,1)
GPIO.setup(p1,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(p2,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(p3,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(p4,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(p5,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(p6,GPIO.IN,pull_up_down=GPIO.PUD_UP)
info=''
press_time=3
count_down=5
def my_callback(pin):
    if(GPIO.input(pinScreen)):
        GPIO.output(pinScreen,GPIO.LOW)
    else:	
        GPIO.output(pinScreen,GPIO.HIGH)
def getCPUtemp():
    global info  
    temp = os.popen('vcgencmd measure_temp').readline()  
    tempfloat = float(temp.replace('temp=','').replace('\'C\n',''))  
    info += 'CPU Temperature is now %.1f Centigrade' %tempfloat  + '\n'
    if tempfloat > 60:  
        info += 'CPU Temperature is too high, pls cool it down'  + '\n'
          
def getCPUusage():
    global info  
    #calculate CPU with two short time, time2 - time1  
    time1 = os.popen('cat /proc/stat').readline().split()[1:5]  
    time.sleep(0.2)  
    time2 = os.popen('cat /proc/stat').readline().split()[1:5]  
    deltaUsed = int(time2[0])-int(time1[0])+int(time2[2])-int(time1[2])  
    deltaTotal = deltaUsed + int(time2[3])-int(time1[3])  
    cpuUsage = float(deltaUsed)/float(deltaTotal)*100  
    info += 'CPU Usage is now %.1f' %cpuUsage +'%'  + '\n'
  
def getRAM():
    global info  
    #get RAM as list,list[7],[8],[9]:total,used,free  
    RAM = os.popen('free').read().split()[7:10]  
    #convert kb in Mb for readablility  
    RAM0 = float(RAM[0])/1024  
    info += 'RAM Total is %.1f MB' %RAM0  + '\n'
    RAM1 = float(RAM[1])/1024  
    percent = RAM1/RAM0*100  
    info += 'RAM Used  is %.1f MB, %.2f' %(RAM1,percent) +'%'+ '\n'  
    RAM2 = float(RAM[2])/1024  
    info += 'RAM Free  is %.1f MB' %RAM2  + '\n'
  
def getDisk():
    global info  
    #get Disk information,DISK[8],[9],[10],[11]:Size, Used. free, Used %  
    DISK = os.popen('df -h /').read().split()[7:12]  
    info += 'Disk total space is %s ' %DISK[0]  + '\n'
    info += 'Disk Used  space is %s ' %DISK[1] +'and is %s' %DISK[3] + '\n'  
    info += 'Disk Free  space is %s ' %DISK[2] + '\n' 
def get_ip():  
    global info    
    info += os.popen("ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{print $1}'").read()
def onPress(channel):
    global press_time,count_down
    count_down=5
    press_time+=1
    if press_time >3:
        press_time=1
    if press_time==1:
        print('system will restart in %s second'%(count_down))
    elif press_time==2:
        print('system will halt in %s second'%(count_down))
    elif press_time==3:
        print 'cancel '
GPIO.add_event_detect(p6,GPIO.FALLING,callback=onPress,bouncetime=300)
GPIO.add_event_detect(p1,GPIO.FALLING,callback=my_callback,bouncetime=300)
while True:
	try:
		time.sleep(1)
		if press_time==1 and count_down==0:
			print "start restart"
			os.system("sudo reboot")
			sys.exit()
		if press_time==2 and count_down==0:
			print "start shutdown"
			os.system("sudo halt")
			sys.exit()
		if press_time==1 or press_time==2:
			count_down-=1
			if press_time==1:
				print("system will restart in %d second"%(count_down))
			elif press_time==2:
				print("system will halt in %d second"%(count_down))
		if press_time==3:
			info = ""    
			getCPUusage()  
			getCPUtemp()
			getRAM()  
			getDisk()
			get_ip()
			uptime=os.popen('uptime').read()[1:-1]
			info+=uptime
			os.system('clear')
			print '-------------SysInfo-----------------'
			print(info)
	except KeyboardInterrupt:
		print('User press Ctrl+c ,exit')
		GPIO.cleanup()
		exit()
